package com.foreverwin.jzymes.job;


import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.quartz.domain.SysJob;
import com.foreverwin.jzymes.qc.point.domain.PointChecklist;
import com.foreverwin.jzymes.qc.point.enums.PointCheckTypeEnum;
import com.foreverwin.jzymes.qc.point.mapper.FindUndoneMapper;
import com.foreverwin.jzymes.qc.point.service.IFindUndoneService;
import com.foreverwin.jzymes.qc.point.service.IPointChecklistService;
import com.foreverwin.jzymes.qc.sample.domain.SampleData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SpotCheckService {
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    FindUndoneMapper findUndoneMapper;
    @Autowired
    IPointChecklistService pointChecklistService;
    @Autowired
    IFindUndoneService findUndoneService;

    /**
     * 点检检查任务
     * @param job
     */
    public void checkSpotcheck( SysJob job ){
        String site = job.getSite();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        Date now = calendar.getTime();
        List<SampleData>  sampleDatas= findUndoneService.findSampleUndone( PointCheckTypeEnum.SAMPLE.getValue() , site ,now);
        List<Map<String,Object>> mapList = findUndoneService.findToolUndone( PointCheckTypeEnum.TOOL.getValue() , site ,"",now );

        //Date now = new Date();
        if ( !sampleDatas.isEmpty() ){

            for ( SampleData sampleData : sampleDatas ){
                PointChecklist pointChecklist = new PointChecklist();
                pointChecklist.setHandle( BOHandleUtil.buildPointCheckHandle());
                pointChecklist.setKey( sampleData.getHandle() );
                pointChecklist.setType( PointCheckTypeEnum.SAMPLE.getValue() );
                pointChecklist.setStatus( sampleData.getStatus() );
                pointChecklist.setCheckResult( "M" );
                pointChecklist.setCheckTime( now );
                pointChecklistService.insertPointChecklist( pointChecklist );
            }
        }

        if ( !mapList.isEmpty() ){

           for ( Map < String ,Object > map : mapList ){

               PointChecklist pointChecklist = new PointChecklist();
               pointChecklist.setHandle( BOHandleUtil.buildPointCheckHandle());
               pointChecklist.setKey( (String) map.get("handle") );
               pointChecklist.setType( PointCheckTypeEnum.TOOL.getValue() );
               pointChecklist.setStatus( (String) map.get("status") );
               pointChecklist.setCheckResult( "M" );
               pointChecklist.setCheckTime( now );
               pointChecklistService.insertPointChecklist( pointChecklist );

           }
        }
    }

}
